package opgave2;

public class Identifier implements IdentifierInterface {

	private StringBuffer data;
	
	public Identifier () {
		data = new StringBuffer();
		data.append('a');
	}
	
	public Identifier (Identifier src) {
		data = new StringBuffer(src.data);
	}
	
	public void init(char c) {
		data = new StringBuffer();
		data.append(c);
	}

	public boolean equals(Identifier ident) {
		return data.toString().compareTo(ident.data.toString()) == 0;
	}

	public char getChar(int n) {
		return data.charAt(n);
	}
	
	public void addChar(char c) {
		data.append(c);
	}

	public int size() {
		return data.length();
	}

	public int compareTo(Identifier id) {
		
		int maxLength = size() > id.size() ? size() : id.size();
		
		for (int i = 0; i < maxLength; i++) {
			if (i >= size()) {
				return 1;
			} else if (i >= id.size()) {
				return -1;
			}
			
			char char1 = getChar(i);
			char char2 = id.getChar(i);
			
			if(char1 > char2) {
				return 1;
			} else if (char1 < char2) {
				return -1;
			}
		}
		
		return 0;
	}
	
	public Identifier clone() {
		Identifier resultaat = new Identifier();
		
		resultaat.init(getChar(0));
		
		for(int i = 1; i < size(); i++) {
			resultaat.addChar(getChar(i));
		}
		
		return resultaat;
	}

	public int compareTo(Object o) {
		Identifier id = (Identifier)o;
		
		int maxLength = size() > id.size() ? size() : id.size();
		
		for (int i = 0; i < maxLength; i++) {
			if (i >= size()) {
				return 1;
			} else if (i >= id.size()) {
				return -1;
			}
			
			char char1 = getChar(i);
			char char2 = id.getChar(i);
			
			if(char1 > char2) {
				return 1;
			} else if (char1 < char2) {
				return -1;
			}
		}
		
		return 0;
	}
}
